package com.lww.feature.test.thread.cyclicbarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
// 赛马马匹
public class Horse implements Runnable {
    // 赛马编号
    private String id;
    // 障碍锁
    private CyclicBarrier cb;
    public Horse(CyclicBarrier cb, String id) {
        this.cb = cb;
        this.id = id;
    }
    @Override
    public void run() {
        try {
            // 赛马出发
            System.out.printf("%s号出发\n", id);
            // 比赛中
//            Thread.sleep(new Random().nextInt(1000));
            // 等待其他赛马到达终点
            cb.await();
            // 所有赛马到达终点，开始统一进行下一场比赛的准备工作
            System.out.printf("%s号准备进行下一场比赛\n", id);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
